Public Class QueryRow
    Inherits UIObject


    Private _Name As String
    Public Property Name As String
        Get
            If String.IsNullOrEmpty(_Name) Then
                If _Formula IsNot Nothing Then
                    If Not _Formula.IsComputed Then
                        If Not String.IsNullOrEmpty(_Aggregate) And _Aggregate <> "(Aucun)" Then
                            Return _Aggregate & "_de_" & _Formula.Formula
                        End If
                        Return _Formula.Formula
                    Else
                        If Not String.IsNullOrEmpty(_Aggregate) And _Aggregate <> "(Aucun)" Then
                            Return _Aggregate & "_de_" & _Formula.Formula
                        End If
                        Return _Formula.Formula
                    End If
                End If
            End If
            Return _Name
        End Get
        Set(ByVal value As String)
            _Name = value
            OnPropertyChanged("Name")
        End Set
    End Property

    Private _Formula As QueryFormula = New QueryFormula()
    Public Property Formula As QueryFormula
        Get
            Return _Formula
        End Get
        Set(ByVal value As QueryFormula)
            _Formula = value
            OnPropertyChanged("Formula")
            OnPropertyChanged("Name")
            OnPropertyChanged("IsComputed")
            OnPropertyChanged("IsNotComputed")
        End Set
    End Property

    Private _Aggregate As String = ""
    Public Property Aggregate As String
        Get
            Return _Aggregate
        End Get
        Set(ByVal value As String)
            _Aggregate = value
            OnPropertyChanged("Aggregate")
            OnPropertyChanged("Name")
        End Set
    End Property

    Public ReadOnly Property IsComputed As Boolean
        Get
            Return _Formula.IsComputed
        End Get
    End Property

    Public ReadOnly Property IsNotComputed As Boolean
        Get
            Return Not _Formula.IsNotComputed
        End Get
    End Property

    Public ReadOnly Property EditFormulaCommand As Windows.Input.ICommand
        Get
            Return New DelegateCommand(Sub()
                                           Dim txt = New Windows.Controls.TextBox()
                                           txt.ContextMenu = New Windows.Controls.ContextMenu()

                                           Dim tablesmi = New Windows.Controls.MenuItem() With {.Header = "Tables"}
                                           For Each dsrc In ModuleDashboarding._activeproject.DataSourceList
                                               Dim mi = New Windows.Controls.MenuItem() With {.Header = dsrc.Name}
                                               For Each col In dsrc.Columns
                                                   Dim micol = New Windows.Controls.MenuItem() With {.Header = col.Name}
                                                   Dim colName = col.Name
                                                   AddHandler micol.Click, Sub() txt.Text = txt.Text.Insert(txt.CaretIndex, colName)
                                                   mi.Items.Add(micol)
                                               Next
                                               tablesmi.Items.Add(mi)
                                           Next
                                           txt.ContextMenu.Items.Add(tablesmi)
                                           txt.Text = Formula.Formula
                                           ModuleDashboarding._uiservice.ShowMessage(txt, "Editeur d'expression")
                                           If Formula.IsNotComputed Then
                                               Formula = New QueryFormula()
                                           End If
                                           Formula.Formula = txt.Text
                                           OnPropertyChanged("Formula")
                                           OnPropertyChanged("Name")
                                           OnPropertyChanged("IsComputed")
                                           OnPropertyChanged("IsNotComputed")
                                       End Sub)
        End Get
    End Property
End Class
